#!/bin/bash

# Copyright (c) 2017, The MesaLock Linux Project Contributors
# All rights reserved.
#
# This work is licensed under the terms of the BSD 3-Clause License.
# For a copy, see the LICENSE file.

set -e

export MAKEFLAGS=-j$(nproc)

workdir="$(pwd)"
buildroot="$workdir/build"
pkgrepo="$workdir/packages"

mkpkg() {
  prepare() { :; }
  build() { :; }
  package() { :; }

  . "$pkgrepo/$1/BUILD"
  
  srcdir="$buildroot/src/$pkgname"
  builddir="$buildroot/bld/$pkgname"
  pkgdir="$buildroot/pkg/$pkgname"
  outdir="$buildroot/out"
  
  rm -rf "$srcdir" "$builddir" "$pkgdir"
  mkdir -p "$srcdir" "$builddir" "$pkgdir" "$outdir"
  
  echo "[+] $1: start"
  for f in prepare build package; do
    echo "[+] $1: $f"
    cd "$buildroot" && "$f"
  done
  
  echo "[+] $1: tar and zip"
  tar cfJ "$outdir/$pkgname.tar.xz" -C "$pkgdir" .
  echo "[+] $1: done"
}

if [[ "$#" -eq 0 ]] ; then
  echo '[+] mkpkg: build all packages'
  ls "$pkgrepo" | parallel --ungroup "-j$(nproc)" ./mkpkg {}
else
  for pkg in "$@"; do
      mkpkg "$pkg"
  done
fi
